using UnityEngine;

public class AudioManager
{
    private AudioSource[] audioSourceArr; //音频组

    public bool IsBGMOpen = true; //是否开启BGM音乐

    public bool IsSFXOpen = true;//是否开启SFX音效

    public AudioManager()
    {
        // audioSourceArr = GameManager.instance ...
        //构造获取GameManager身上的两个音频源

        audioSourceArr = GameManager.instance.GetComponents<AudioSource>();
    }

    /// <summary>
    /// 播放BGM
    /// </summary>
    /// <param name="clip">音频片段</param>
    /// <param name="volume">音量</param>
    public void PlayBGMusic(AudioClip clip , float volume = 0.5f)
    {
        if ( !audioSourceArr[0].isPlaying || audioSourceArr[0].clip != clip )
        {
            audioSourceArr[0].clip = clip;
            audioSourceArr[0].volume = volume;
            audioSourceArr[0].Play();
        }
    }

    /// <summary>
    /// 播放音效
    /// </summary>
    /// <param name="clip">音频片段</param>
    /// <param name="volume">音量</param>
    public void PlaySFXMusic(AudioClip clip , float volume = 0.5f)
    {
        if ( IsSFXOpen )
        {
            audioSourceArr[1].PlayOneShot(clip , volume);
        }
    }

    /// <summary>
    /// BGM音乐按钮回调
    /// </summary>
    public void OnBGMButtonClick()
    {
        IsBGMOpen = !IsBGMOpen;
        if ( IsBGMOpen )
        {
            audioSourceArr[0].Play();
        }
        else
        {
            audioSourceArr[0].Stop();
        }
    }

    /// <summary>
    /// SFX音效按钮回调
    /// </summary>
    public void OnSFXButtonClick()
    {
        IsSFXOpen = !IsSFXOpen;
    }

    /// <summary>
    /// 开启BGM音乐
    /// </summary>
    public void OpenBGM()
    {
        audioSourceArr[0].Play();
    }

    /// <summary>
    /// 关闭BGM音乐
    /// </summary>
    public void CloseBGM()
    {
        audioSourceArr[0].Stop();
    }
}